/**
 * 2013-6-2 上午7:59:03 Created By niexiaoqiang
 */

package com.xiaoqiang.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import android.os.Environment;

/**
 * perperties文件
 */
public class MtwProperties {
	public static Properties props;
	private static MtwProperties instance = null;

	private String sdPath;
	private String profilepath;

	private MtwProperties() throws IOException {

		sdPath = Environment.getExternalStorageDirectory().getPath();
		profilepath = sdPath + "/mtw/mtw.properties";

		props = new Properties();
		File proFile = new File(profilepath);
		File parent = proFile.getParentFile();
		if (null != parent && !parent.exists()) {
			parent.mkdirs();
		}
		if (!proFile.exists()) {
			proFile.createNewFile();
		}
		props.load(new BufferedInputStream(new FileInputStream(profilepath)));
	}

	public static synchronized MtwProperties newInstance() throws IOException {
		if (instance == null) {
			instance = new MtwProperties();
		}
		return instance;
	}

	public String getProperties(String proName) {
		if (null == proName) {
			return null;
		}
		String value = props.getProperty(proName);
		if (null == value || "" == value) {
			return null;
		}
		return value.trim();
	}

	public void setProperties(String proName, String value) throws FileNotFoundException {
		if (null == proName || null == value) {
			return;
		}
		props.setProperty(proName, value);
		props.save(new FileOutputStream(new File(profilepath)), props.toString());
	}
}
